<template>
  <el-form :model="user" label-width="120px">
    <el-form-item label="昵称" size="large" style="width: 20%">
      <el-input v-model="user.nickName" :disabled = "bl"/>
    </el-form-item>
    <el-form-item label="创建日期">
      <div class="block">
        <el-date-picker
            v-model="user.createTime"
            type="datetime"
            placeholder="Select date and time"
            value-format="YYYY-MM-DD HH:mm:ss"
            :disabled = "bl"
        />
      </div>
    </el-form-item>
    <el-form-item label="邮箱">
      <el-input v-model="user.email" style="width: 30%" :disabled = "bl" />
    </el-form-item>
    <el-form-item label="性别">
      <el-radio-group v-model="user.sex" :disabled = "bl">
        <el-radio label="男" />
        <el-radio label="女" />
      </el-radio-group>
    </el-form-item>
    <el-form-item label="个性签名">
      <el-input v-model="user.signature" type="textarea" placeholder="你暂时没有签名" :disabled="bl"/>
    </el-form-item>
    <el-form-item v-if="bl==true">
      <el-button type="primary"  @click="bl = false">修改</el-button>
    </el-form-item>
    <el-form-item v-if="bl==false">
      <el-button type="primary"  @click="updateUser">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive ,ref,inject} from 'vue'
import {onMounted} from "vue-demi";
import request from "@/utils/request";
import {ElMessage} from 'element-plus'
import router from "@/router";

const reload = inject("reload")
let user = ref({})

let bl = ref(true)
onMounted(async ()=>{
  let result = await request.get("user/getuser")
  let data = result.data
  if(data.code==200){
    user.value = data.data
  }else{
    open("请重新登录","success")
    await router.push({path:"/"})
  }
})
const open = (msg,type) => {
  ElMessage({
    message: msg,
    type: type,
  })
}
const updateUser = async ()=>{
  let result = await request.post("user/updateuser",user.value)
  let data = result.data
  if(data.code===200){
    open(data.msg,"success")
    reload()
  }else {
    await router.push({path:"/"})
    open(data.msg,"error")
  }
}
</script>
